var config = require('../../utils/config.js');
var netutil = require('../../utils/netutil.js');
var baseutil = require('../../utils/baseutil.js');
let { WeToast } = require('../../src/wetoast.js');
var payutil = require('../../utils/LCPayWorking.js');
var register = require('../../utils/refreshLoadRegister.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currtotal: 1,
    currentTab: 0,//0国内机票  1火车票
    currentOrderList: null,
    currtotal: 1,
    curr0: 1,
    total0: 1,
    OrderList0: new Array(),
    curr1: 1,
    total1: 1,
    OrderList1: new Array(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var that = this;       /**    * 获取系统信息    */
    // wx.getSystemInfo({
    //   success: function (res) {
    //     that.setData({
    //       winWidth: res.windowWidth, winHeight: res.windowHeight
    //     });
    //   }
    // });
    register.register(this);
    this.setData({
      TUserInfo: app.globalData.TUserInfo,
      currentTab: options.type,
    })
    this.getOrderList()
    //创建可重复使用的toast实例，并附加到this上，通过this.wetoast访问
    new app.WeToast()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //模拟刷新数据
  refresh: function () {
    console.log("刷新数据!")

    if (this.data.currentTab == 0) {
      this.data.curr0 = 1;
    } else if (this.data.currentTab == 1) {
      this.data.curr1 = 1;
    }

    this.getOrderList();
  },
  //模拟加载更多数据
  loadMore: function () {
    if (this.data.currentTab == 0) {
      if (this.data.currtotal < this.data.total0) {
        this.data.curr0++;
        this.getOrderList();
      } else {
        register.loadFinish(this, true);
        this.wetoast.toast({
          title: '没有更多数据了',
          duration: 1000
        })
      }
    } else if (this.data.currentTab == 1) {
      if (this.data.currtotal < this.data.total1) {
        this.data.curr1++;
        this.getOrderList();
      } else {
        register.loadFinish(this, true);
        this.wetoast.toast({
          title: '没有更多数据了',
          duration: 1000
        })
      }
    }

  },
  getOrderList: function () {
    var THIS = this
    var url = null;
    var Data = new Object();
    if (THIS.data.currentTab == 0) {
      url = config.getBaseUrl("FlightApi", "GetOrderList");
      Data.OASiteID = this.data.TUserInfo.SiteID
      Data.EID = this.data.TUserInfo.EID
      Data.BookUserID = this.data.TUserInfo.UserID
      Data.UserID = this.data.TUserInfo.UserID
    } else if (THIS.data.currentTab == 1) {
      url = config.getBaseUrl("Train", "GetOrderList");
      Data.SiteID = this.data.TUserInfo.SiteID
      Data.Eid = this.data.TUserInfo.EID
      Data.UserID = this.data.TUserInfo.UserID
    }

    Data.PageSize = "15"
    if (this.data.currentTab == 0) {
      Data.PageIndex = this.data.curr0;
    } else if (this.data.currentTab == 1) {
      Data.PageIndex = this.data.curr1;
    }

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var Data = res.data.Data
          var OrderList = null
          if (THIS.data.currentTab == 0) {
            var DataOrder =Data.DataOrder
            for (var i = 0; i < DataOrder.length;i++){
              var AirLines = DataOrder[i].AirLines
              for (var j = 0; j < AirLines.length;j++){
                AirLines[j].BeginDateText = AirLines[j].BeginDate.slice(5, AirLines[j].BeginDate.length-3).replace("/","-");
                AirLines[j].ArriveDateText = AirLines[j].ArriveDate.slice(5, AirLines[j].ArriveDate.length-3).replace("/", "-");
              }

            }
              
            THIS.setData({
              total0: Data.NumRecord,
            })
            OrderList = Data.DataOrder
          } else if (THIS.data.currentTab == 1) {
            var OrderList = Data.OrderList
            for (var i = 0; i < OrderList.length; i++) {
                         
              OrderList[i].BeginDateText = OrderList[i].StartDateTime.slice(5, OrderList[i].StartDateTime.length-3).replace("/", "-");
              OrderList[i].ArriveDateText = OrderList[i].EndTime.slice(5, OrderList[i].EndTime.length-3).replace("/", "-");
              

            }
            THIS.setData({
              total1: Data.TotalRecord,
            })
            OrderList = Data.OrderList
          }

          if (OrderList.length > 0) {
            if (THIS.data.currentTab == 0) {
              if (THIS.data.curr0 == 1) {
                console.log("第一次添加")
                THIS.setData({
                  OrderList0: OrderList,
                  currentOrderList: OrderList,
                  currtotal: OrderList.length
                })
              } else {
                console.log("第二次添加")
                var temp = THIS.data.OrderList0.concat(OrderList);
                THIS.setData({
                  OrderList0: temp,
                  currentOrderList: temp,
                  currtotal: temp.length
                })
              }
            } else if (THIS.data.currentTab == 1) {
              if (THIS.data.curr1 == 1) {
                console.log("第一次添加")
                THIS.setData({
                  OrderList1: OrderList,
                  currentOrderList: OrderList,
                  currtotal: OrderList.length
                })
              } else {
                console.log("第二次添加")
                var temp = THIS.data.OrderList1.concat(OrderList);
                THIS.setData({
                  OrderList1: temp,
                  currentOrderList: temp,
                  currtotal: temp.length
                })
              }
            }
          } else {
            if (THIS.data.currentTab == 0 && THIS.data.curr0 == 1) {
              THIS.setData({
                currtotal: 0
              })
            } else if (THIS.data.currentTab == 1 && THIS.data.curr1 == 1) {
              THIS.setData({
                currtotal: 0
              })
            } else {
              THIS.setData({
                currtotal: THIS.data.currentOrderList == null ? 0 : THIS.data.currentOrderList.length
              })
            }


          }
          register.loadFinish(THIS, true);
        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
          register.loadFinish(THIS, true);
        }
      })
  },
  /**   * 点击tab切换   */
  swichNav: function (e) {

    if (this.data.currentTab == e.currentTarget.dataset.current) {
      return false;
    } else {

      this.setData({
        currentTab: e.currentTarget.dataset.current
      })
      console.log(this.data.currentTab + "=this.data.currentTab")
      if (this.data.currentTab == 0) {
        if (this.data.OrderList0 == null || this.data.OrderList0 == '' || this.data.OrderList0.length == 0) {
          this.refresh();
        }
        this.setData({
          currentOrderList: this.data.OrderList0,
          currtotal: this.data.OrderList0.length
        })

      } else if (this.data.currentTab == 1) {
        if (this.data.OrderList1 == null || this.data.OrderList1 == '' || this.data.OrderList1.length == 0) {
          this.refresh();
        }
        this.setData({
          currentOrderList: this.data.OrderList1,
          currtotal: this.data.OrderList1.length
        })

      }
    }
  },
  goOrderDetail(e){
    var position = e.currentTarget.dataset.position
    var orderData = this.data.currentOrderList
    baseutil.log(orderData)
    if (this.data.currentTab==0){//机票
      wx.navigateTo({
        url: '../approvalDetail/approvalDetail?OrderNo=' + orderData[position].OrderBase.OrderNO + "&OrderType=" + 1 +"&isFrom=1"
      })
    }else{
      wx.navigateTo({
        url: '../approvalDetail/approvalDetail?OrderNo=' + orderData[position].OrderNo + "&OrderType=" + 3 + "&isFrom=1"
      })
    }
  
  },
  cancelOrder(e) {
    var position = parseInt(e.currentTarget.dataset.position)
    var orderDetail=this.data.currentOrderList[position]
    baseutil.log(orderDetail)
    var THIS = this
    var url = null
    var Data = new Object();
    if (this.data.currentTab == 0) {
      url = config.getBaseUrl("FlightApi", "CancelOrder");
      Data.Remark = ""
      Data.UserTypeText = "员工";//员工
      Data.UserNumber = app.globalData.TUserInfo.TrueName;//员工姓名
      Data.OrderNo = orderDetail.OrderBase.OrderNO
      
    } else {
      url = config.getBaseUrl("Train", "CancelOrder");
      Data.OrderNo = orderDetail.OrderNo
    }
    Data.UserID = app.globalData.TUserInfo.UserID
    Data.SiteID = app.globalData.TUserInfo.SiteID


    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (THIS.data.currentTab == 0) {
          if (res.data.Data==null){
            THIS.wetoast.toast({
              title: res.data.ResponseMetaInfo.Reason,
              duration: 1000
            })
          }else{
            if (res.data.Data.IsSuccess) {
              var tempData = res.data.Data
              THIS.wetoast.toast({
                title: "订单已取消",
                duration: 1000
              })
              THIS.refresh()
            } else {
              THIS.wetoast.toast({
                title: res.data.Data.Message,
                duration: 1000
              })
            }
          }
          

        } else {
          if (res.data.ResponseMetaInfo.StatusCode == '0') {
            var tempData = res.data.Data
            THIS.wetoast.toast({
              title: "订单已取消",
              duration: 1000
            })
            THIS.refresh()
          } else {
            THIS.wetoast.toast({
              title: res.data.ResponseMetaInfo.Reason,
              duration: 1000
            })
          }
        }


      })
  },
  goPay(e) {
    var position = parseInt(e.currentTarget.dataset.position)
    
    var OrderDetail = this.data.currentOrderList[position]
    baseutil.log(OrderDetail)
    var OrderType = this.data.currentTab
 

    var payorderno = OrderType == 0 ? OrderDetail.OrderBase.OrderNO : OrderDetail.OrderNo
    var money = OrderType == 0 ? OrderDetail.OrderBase.OrderAmount : OrderDetail.OrderAmount
    var transactionType = OrderType == 0 ? 1 : 5
    var subTransactionType = OrderType == 0 ? 1 : 11
    var fromWhere = OrderType == 0 ? 1 : 0
    baseutil.log("---")
    baseutil.log("payorderno=" + payorderno + ",money=" + money + ",transactionType=" + transactionType + ",subTransactionType=" + subTransactionType + ",fromWhere=" + fromWhere)
    baseutil.log("---")
    payutil.getPayInfo(payorderno,
      money,
      transactionType,
      subTransactionType,
      fromWhere,1);

  }
})